package com.sherlockHolmes.init.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sherlockHolmes.init.entity.dao.RolePerm;
import com.sherlockHolmes.init.entity.dao.UserRole;
import com.sherlockHolmes.init.service.RolePermService;
import com.sherlockHolmes.init.mapper.RolePermMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 四月一日 君寻
* @description 针对表【role_perm】的数据库操作Service实现
* @createDate 2024-03-25 16:49:10
*/
@Service
public class RolePermServiceImpl extends ServiceImpl<RolePermMapper, RolePerm>
    implements RolePermService{
    @Resource
    private RolePermMapper rolePermMapper;

    /**
     * 获取角色权限列表
     * @param role
     */
    @Override
    public List<Integer> getPerMissionsList(List<UserRole> role) {
        List<Integer> roleList = role.stream().map(UserRole::getRole).collect(Collectors.toList());
        if(ObjectUtil.isEmpty(roleList)||roleList.size()<=0){
            throw new RuntimeException("用户未分配角色");
        }
        List<RolePerm> list = new LambdaQueryChainWrapper<RolePerm>(rolePermMapper)
                .in(RolePerm::getRoleId, roleList)
                .list();
        Set<Integer> collect = list.stream().map(RolePerm::getPermId).collect(Collectors.toSet());
        List<Integer> integers = new ArrayList<>(collect);
        return integers;
    }
}




